<template>
  <div class="comment-section">
    <h3 class="section-title">
      {{ title }} ({{ totalComments }})
    </h3>

    <!-- 评论输入框 -->
    <div class="comment-input-box">
      <el-input
        v-model="commentText"
        type="textarea"
        :rows="3"
        :placeholder="placeholder"
        maxlength="500"
        show-word-limit
      />
      <el-button
        type="primary"
        class="submit-btn"
        :loading="submitting"
        @click="handleSubmit"
      >
        发表评论
      </el-button>
    </div>

    <!-- 评论列表 -->
    <div v-if="comments.length > 0" class="comments-list">
      <div v-for="comment in comments" :key="comment.id" class="comment-item">
        <img
          :src="comment.user.avatar"
          :alt="comment.user.name"
          class="comment-avatar"
        />
        <div class="comment-content">
          <div class="comment-header">
            <span class="user-name">{{ comment.user.name }}</span>
            <span class="comment-time">{{ comment.time }}</span>
          </div>
          <p class="comment-text">{{ comment.content }}</p>
          <div class="comment-actions">
            <span class="action-btn" @click="handleReply(comment)">
              <el-icon><ChatDotRound /></el-icon>
              回复
            </span>
            <span
              class="action-btn"
              :class="{ active: comment.isLiked }"
              @click="handleLikeComment(comment)"
            >
              <el-icon><Star /></el-icon>
              {{ comment.likes }}
            </span>
          </div>

          <!-- 回复列表 -->
          <div v-if="comment.replies?.length" class="replies-list">
            <div
              v-for="reply in comment.replies"
              :key="reply.id"
              class="reply-item"
            >
              <img
                :src="reply.user.avatar"
                :alt="reply.user.name"
                class="reply-avatar"
              />
              <div class="reply-content">
                <div class="reply-header">
                  <span class="user-name">{{ reply.user.name }}</span>
                  <span v-if="reply.replyTo" class="reply-to">
                    回复 @{{ reply.replyTo }}
                  </span>
                  <span class="reply-time">{{ reply.time }}</span>
                </div>
                <p class="reply-text">{{ reply.content }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-else class="empty-comments">
      <el-icon class="empty-icon"><ChatDotRound /></el-icon>
      <p class="empty-text">暂无评论，快来发表第一条评论吧~</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { ChatDotRound, Star } from '@element-plus/icons-vue'

const props = defineProps({
  // 评论列表
  comments: {
    type: Array,
    default: () => []
  },
  // 总评论数
  totalComments: {
    type: Number,
    default: 0
  },
  // 标题
  title: {
    type: String,
    default: '评论'
  },
  // 输入框占位符
  placeholder: {
    type: String,
    default: '写下你的评论...'
  }
})

const emit = defineEmits(['submit', 'reply', 'like'])

const commentText = ref('')
const submitting = ref(false)

// 提交评论
const handleSubmit = async () => {
  if (!commentText.value.trim()) {
    ElMessage.warning('请输入评论内容')
    return
  }

  submitting.value = true
  try {
    await emit('submit', commentText.value)
    commentText.value = ''
    ElMessage.success('评论成功')
  } catch (error) {
    ElMessage.error('评论失败，请重试')
  } finally {
    submitting.value = false
  }
}

// 回复评论
const handleReply = (comment) => {
  commentText.value = `@${comment.user.name} `
  emit('reply', comment)
}

// 点赞评论
const handleLikeComment = (comment) => {
  emit('like', comment)
}
</script>

<style scoped lang="less">
.comment-section {
  margin-top: 48px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 24px 0;
}

// 评论输入框
.comment-input-box {
  margin-bottom: 32px;

  .submit-btn {
    margin-top: 12px;
  }
}

// 评论列表
.comments-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.comment-item {
  display: flex;
  gap: 16px;
}

.comment-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.comment-content {
  flex: 1;
  min-width: 0;
}

.comment-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;

  .user-name {
    font-size: 14px;
    font-weight: 600;
    color: #1a1a1a;
  }

  .comment-time {
    font-size: 13px;
    color: #999;
  }
}

.comment-text {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin: 0 0 12px 0;
  word-break: break-word;
}

.comment-actions {
  display: flex;
  gap: 20px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: #999;
  cursor: pointer;
  transition: color 0.3s;

  &:hover {
    color: #ff6b6b;
  }

  &.active {
    color: #ff6b6b;
  }
}

// 回复列表
.replies-list {
  margin-top: 16px;
  padding-left: 16px;
  border-left: 2px solid #f0f0f0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.reply-item {
  display: flex;
  gap: 12px;
}

.reply-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.reply-content {
  flex: 1;
  min-width: 0;
}

.reply-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;

  .user-name {
    font-size: 13px;
    font-weight: 600;
    color: #1a1a1a;
  }

  .reply-to {
    font-size: 13px;
    color: #ff6b6b;
  }

  .reply-time {
    font-size: 12px;
    color: #999;
  }
}

.reply-text {
  font-size: 13px;
  color: #666;
  line-height: 1.6;
  margin: 0;
  word-break: break-word;
}

// 空状态
.empty-comments {
  text-align: center;
  padding: 60px 20px;
  color: #999;

  .empty-icon {
    font-size: 64px;
    color: #ddd;
    margin-bottom: 16px;
  }

  .empty-text {
    font-size: 14px;
    margin: 0;
  }
}

// 响应式
@media (max-width: 768px) {
  .comment-item {
    gap: 12px;
  }

  .comment-avatar {
    width: 36px;
    height: 36px;
  }

  .replies-list {
    padding-left: 12px;
  }

  .reply-avatar {
    width: 28px;
    height: 28px;
  }
}
</style>
